package com.kkb.kkbportal.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import com.kkb.kkbportal.domain.ums.User;
import com.kkb.kkbportal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "用户管理", description = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录", notes = "登录接口")
    @ApiImplicitParam(name = "condition", value = "用户登录所需信息", dataType = "User")
    @PostMapping("/login")
    public CommonResult<User> login(@RequestBody @Validated User condition) {
        userService.userLogin(condition);
        return CommonResult.success(null, "登录成功");
    }

    @ApiOperation(value = "注册", notes = "注册接口")
    @ApiImplicitParam(name = "condition", value = "用户注册所需信息", dataType = "User")
    @PostMapping("/register")
    public CommonResult<User> register(@RequestBody @Validated User user) {
        userService.userRegister(user);
        return CommonResult.success(null, "注册成功");
    }

    @ApiOperation(value = "用户信息分页查询", notes = "用户分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition", value = "用户筛选条件", dataType = "User"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", defaultValue = "10", dataType = "int")
    })
    @PostMapping("/query/page")
    public CommonResult<CommonPage<User>> listPage(@RequestBody User condition,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> page = userService.selectPage(condition, pageNum, pageSize);
        System.out.println(page.getList());
        CommonPage<User> commonPage = CommonPage.restPage(page.getList());
        return CommonResult.success(commonPage, "查询成功");
    }

    @ApiOperation(value = "根据用户id添加角色", notes = "用户-角色关系添加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "long"),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "long")
    })
    @PostMapping("/add/role")
    public ResponseEntity<String> selectRoleByUserId(@RequestParam("userId") Long userId, @RequestParam("roleId") Long roleId) {
        userService.selectRoleByUserId(userId, roleId);
        return ResponseEntity.ok("添加用户角色关系成功");
    }

    @ApiOperation(value = "根据用户id查找用户信息", notes = "根据id查询用户信息接口")
    @ApiImplicitParams(@ApiImplicitParam(name = "id", value = "用户id", dataType = "long"))
    @GetMapping("/{id}")
    public CommonResult<User> selectUserById(@PathVariable("id") Long id) {
        User user = userService.selectUserById(id);
        return CommonResult.success(user, "查询成功");
    }

    @ApiOperation(value = "根据用户id修改用户", notes = "用户修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "long"),
            @ApiImplicitParam(name = "user", value = "用户id", dataType = "User")
    })
    @PutMapping("/update/{id}")
    public CommonResult<User> updateUserById(@PathVariable("id") Long id, @RequestBody User user) {
        userService.updateById(id, user);
        return CommonResult.success(null, "修改成功");
    }

    @ApiOperation(value = "根据用户id进行逻辑删除", notes = "用户删除接口")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "long")
    @DeleteMapping("/delete/{id}")
    public CommonResult<User> deleteUserById(@PathVariable("id") Long id) {
        userService.deleteById(id);
        return CommonResult.success(null, "删除成功");
    }

    @ApiOperation(value = "用户信息列表查询", notes = "用户列表查询接口")
    @ApiImplicitParam(name = "condition", value = "用户筛选条件", dataType = "User")
    @PostMapping("/query/list")
    public CommonResult<List<User>> list(@RequestBody User condition) {
        List<User> page = userService.selectByCondition(condition);
        System.out.println(page);
        return CommonResult.success(page, "查询成功");
    }

    @ApiOperation(value = "发送验证码", notes = "用户点击获取验证码接口")
    @ApiImplicitParam(name = "phoneNumber", value = "用户手机号", dataType = "String")
    @PostMapping("/password")
    public ResponseEntity<String> sendSMSCode(@RequestParam("phoneNumber") String phoneNumber) {
        userService.forgetPassword(phoneNumber);
        return ResponseEntity.ok("发送验证码成功");
    }

    @ApiOperation(value = "根据验证码验证账号", notes = "验证码登录-用户点击确定接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "用户手机号", dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String")
    })
    @PostMapping("/password/verification")
    public CommonResult<User> sendSMSCode(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("code") String code) {
        User user = userService.smsCodeVerify(phoneNumber, code);
        return CommonResult.success(user, "验证码登录成功");
    }
}
